<?php

namespace App\Service\Feishu\Event\Hire\Application;

use App\Models\Common\ConfigModel;
use App\Models\Feishu\FeishuHireApplicationModel;
use App\Models\Kms\User\KmsUser;
use App\Service\Feishu\Event\EventAbstractService;
use App\Service\Feishu\Hire\ApplicationService;

/**
 * 投递阶段变更
 */
class StageChangedV1 extends EventAbstractService
{
    public function handle()
    {

        // 已经不用了, 作废
        return ;
        $applicationId = $this->eventData['application_id'] ?? '';
        if (!$applicationId) {
            return;
        }
        // 通过 application 获取 获取投递信息
        $appId = ConfigModel::getFsConfigByPlatformNameOrgId('zhaopin', 0)['appId'] ?? '';

        $hireService     = new ApplicationService($appId);
        $applicationInfo = $hireService->getApplicationInfo($applicationId);
        $applicationInfo = $applicationInfo['application'];

        // 重置历史数据
        FeishuHireApplicationModel::query()->where('applicationId', $applicationId)->update(['isLatest' => 0]);

        $applicationCreateData = [
            'applicationId' => $applicationId,
            'talentId'      => $applicationInfo['talent_id'],
            'stageId'       => $applicationInfo['stage']['id'],
            'stageName'     => $applicationInfo['stage']['zh_name'],
            'stageType'     => $applicationInfo['stage']['type'],
            'activeStatus'  => $applicationInfo['active_status'],
            'isLatest'      => 1,
            'originalData'  => json_encode($applicationInfo),
            'createdAt'     => (int)($applicationInfo['create_time'] / 1000),
            'createdByUid'  => 0,
            'updatedAt'     => (int)($applicationInfo['modify_time'] / 1000),
            'updatedByUid'  => 0,
        ];

        // 拿到了人才 ID, 去获取人才信息接口
        $talentInfo = $hireService->getTalentInfo($applicationInfo['talent_id']);

        $creatorId = $talentInfo['talent']['basic_info']['creator_id'] ?? '';

        if (empty($creatorId)) {
            $fsUserInfo = [
                'name'     => '系统',
                'nickname' => '夸父AI'
            ];
        } else {
            $fsUserInfo = KmsUser::query()->where('userId', $creatorId)->first();
        }

        $applicationCreateData['talentCreatorFsId']       = $creatorId;
        $applicationCreateData['talentCreatorFsName']     = $fsUserInfo['name'];
        $applicationCreateData['talentCreatorFsNickname'] = $fsUserInfo['nickname'];;

        // 存入数据库
        FeishuHireApplicationModel::query()->create($applicationCreateData);
    }
}
